<?php
header("Content-type:text/html;charset=utf-8");
/** 
* 验证签名 
* TobeVerified 待验证签名的密文 
* PlainText 待验证签名的明文 
* CertFile 签名者公钥证书 
* return 验证成功返回true，失败返回false(从LastErrMsg属性获取失败原因) 
*/
function VerifyMsg($TobeVerified, $PlainText, $CertFile,$signature_alg=OPENSSL_ALGO_SHA1)
{
//用公钥验签
    $fp=fopen($CertFile,"r");
    if(!$fp)
    {

//echo "Error Number:-10005, Error Description: ER_FIND_CERT_FAILED（找不到证书）";
        return false;
    }
    $pub_key=fread($fp,8192);
    fclose($fp);
    $res = openssl_get_publickey($pub_key);
    if (1==openssl_verify($PlainText,pack("H" . strlen($TobeVerified), $TobeVerified) , $res,$signature_alg))
    {

//print("www.111cn.net提示您：验证成功"." <br>");
        return true;
    }
    else
    {

//echo "Error Number:-10021, Error Description: ER_VERIFY_ERROR（验签失败）|".openssl_error_string(); 
        return false;
    }

}


//------- RSA验签
/*$str = 'oC3AH2wHOVGNMLLxO78/EfTThl/PkVbEqcF16toM5Cy4Y5+j/RvPy+fjFooP+FOUcLzaRBodFMGSDbWbHLB4UrrwqsTnz4wp7MyCe5VcXiZUK/a9Ke42PtJrQw2jhwIxA3jMB4px9DdiD/FO0fhSEzhqhZu/HDugu0ZR7Tvl0IY=';

//公钥字符串
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrheUnSS/KUnumoqvjEgRVWemA
FlEXQY1Ke56KtBWCfHvwUOULClxx+nCNkvNXDDKA/+ATsbDMD8/Z+Z2bxbcMQ5bO
86JzdchGI8y+n+lCBTPzyFAVQdaA/s+2KP73wy3hCCZlK0QOkuOvyaABntm8nItz
AWNNm32EMG3A6EbDJwIDAQAB
-----END PUBLIC KEY-----";

//公钥字符串转化资源
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
echo '<pre><br>key:';var_dump($pu_key);
//获取到回调数据
//解密密码，为空（不可更改）
$decrypted = "";
//rsa解密
openssl_public_decrypt(base64_decode($str), $decrypted, $pu_key);//私钥加密的内容通过公钥可用解密出来
$data = json_decode($decrypted, true);
echo '<br>data:';var_dump($data);*/



//------- 公钥验签
//$str = '75swIaQIHm1vhB4YR6g5I1UVKmVIR7g8lEGG1tkPzyp+aWX6U/L7t7QqPVovsNZpbeep1Iv1+0a4It+0f/lBWFLiZbDekA0VKbmI/Nlvi0+8nwBJywSsnrXbSUbuNZueXFeXKghxNiVWAkc7ZnlrYsXYDfGpfPMAIIrfgRpEKRI=';

//公钥字符串
/*$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvpBDYeWK1VGZCwETz5MHSzmZa
g8zgDDH89JqgJ1EUDThBrHYlbksR3ACORfvDh6J+jSKWoXIQJcS12ElyqzwUaTpT
thnUh0jU8mJgOzJuTwtk2xZKNEk71M098h4tmriJCPMWEHMn+0U/qCoHSdzbftNw
XTpjRrf6L4PyFAeePQIDAQAB
-----END PUBLIC KEY-----";

$data = array(
	"orderid" => "appstorehw1rx_a970f06f-943c-4202-9605-41d5d86898bb",
    "transid" => "20170727124036279204124905635961",
    "channel" => "1",
    "appid" => "1769",
    "ordername" => "3000元宝",
    "price" => "0.02",
    "userid" => "355e0572364c25cb3040adb0899d1adf",
    "username" => "355e0572364c25cb3040adb0899d1adf",
    "attach" => "99994",
    "status" => "5",
    "sandbox" => "0",
    "createat" => "2017-07-27 12:40:36",
    "startat" => "2017-07-27 12:40:38",
    "payat" => "2017-07-27 12:40:49",
    "sign" => "QN7vasm4ruhiXFrWElB1nzv1gT8JCul56xBSaevlq5V3rNNACOOrEA/x42qKOUCEpQ3BLahQqWsCgvXfl2cTYe5cCQtMtGcvC7iHO3nSp6Fxpar170SPWGoiY/6nHHBmeu7ls/8Wvgx6/n616MmkYPZ0pibOFPPKBCBovFHxA44=",
);
echo '<br>data:';var_dump($data);
// 拼接待签名字符串
$signStr = '';
foreach ($data as $k => $v) {
	$signStr .= '&'.$k.'='.$v;
}
$signStr = ltrim($signStr, '&');
//公钥字符串转化资源
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
echo '<pre><br>key:';var_dump($pu_key);
$result = (bool)openssl_verify($signStr, base64_decode($str), $pu_key, OPENSSL_ALGO_SHA1);
echo '<br>result:';var_dump($result);*/



//------- DES验签
$sign = 'DeOp8go3XY7Fk34Hf08xNSyBxA8ru5uONbUIwpYSGCa6frHfP6nfdGafhQqLOrBfgPTgQ6dcuGlO8g+vBTiC/9ZO9skdOLFR8hUP6UY7UVUkCmB75e1GAjG06Roacc4Uv5sanQA+O82+1NE0X0OzLMIZf05qeU93rIwhLVzyzPo=';
echo '<br>sign:<br>';print_r($sign);
echo '<br>strlen='.strlen($sign);
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrheUnSS/KUnumoqvjEgRVWemA
FlEXQY1Ke56KtBWCfHvwUOULClxx+nCNkvNXDDKA/+ATsbDMD8/Z+Z2bxbcMQ5bO
86JzdchGI8y+n+lCBTPzyFAVQdaA/s+2KP73wy3hCCZlK0QOkuOvyaABntm8nItz
AWNNm32EMG3A6EbDJwIDAQAB
-----END PUBLIC KEY-----";
//公钥字符串转化资源
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
//echo '<br>pu_key:';var_dump($pu_key);
//获取到回调数据
//解密密码，为空（不可更改）
$decrypted = "";
//RSA解密
openssl_public_decrypt(base64_decode($sign),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来 
echo '<br><br>解密的字串:<br>';var_dump($decrypted);



//------- test
/*$str = 'VpZHjhEj0G2GO/pMYyF0dGrXgPPcXSv1C0k/bjsafEDCodrkeHSrYWYZReEhBGZXs/6sVbnfuHcGVT9mLLUtUPy+HHnTnttjVKmm9jZbOYRgQ9XUepJs0FDufBUv+yNtOaQ3IrmLJeTCfGmS1o0kFTsnum8LcFNGY7SI4ZgwDAihCDXzfrQP4nTZ5E8C2LehQxS8KAx9hXxeEhZIfhBMRiOUh+mX8/M4SdX6zvOuX6boFTr2G/2vlJP6eejJuRkKqHJ6sLP8skkQynhWZGLFtpCWzINPWJ3+an8L0/blBZSDaKwWgC7GUTVVoysheDIGyt3291gR8BjoH8L3WJt+og==';
//公钥字符串
$public_key = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5SCpdyndz+nd9NUBSTKA
Klo9AdFxH0FaaoOHE+fI7yLPktyeIfNJr1fkbolyO11WHxnyK0dZbUupq3skjDJV
sr89QXMWm+cj54RcBOObfUU9zoZ/9lDGyoCaKPesADuWCf5zz89Km9hByV6ZuFcN
BlyRBmQDqqASfVgltMpXyDgt9ErPDdn+I+6y6OOO1N0tLT3U/skOh6zoU/zfyVtV
hSNm1+2Vv3IWqQb7YMwB1AUPke8VFejTXxE/pq3rKkQ0IO2qNOYmJTOs+V3r1/OO
Llxq4Bd82sFCZKf9VlZweOfisj4sBKJ+of6meV2LOm5zN/Xc1281wE3eln1aeWCZ
JQIDAQAB
-----END PUBLIC KEY-----";
//公钥字符串转化资源
$pu_key = openssl_pkey_get_public($public_key);
echo '<br>pu_key:';var_dump($pu_key);
//获取到回调数据
//解密密码，为空（不可更改）
$decrypted = "";
//rsa解密
openssl_public_decrypt(base64_decode($str), $decrypted, $pu_key);//私钥加密的内容通过公钥可用解密出来
echo '<br><br>解密的字串:<br>'.$decrypted,"\n";
$data = json_decode($decrypted, true);*/








?>